package com.jk.controller;

import com.jk.api.OrderApi;
import com.jk.po.OrderPO;
import com.jk.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/***
 * @ClassName: OrderController
 * @Description:TODO
 * @Auther: 景晨璇
 * @Date: 2021/6/30 15:01
 * @version : V1.0
 */
@Controller
public class OrderController implements OrderApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<OrderPO> queryOrder() {
        return mongoTemplate.findAll(OrderPO.class);
    }
    /**
    * @Author 景晨璇
    * @method  queryOrderById
    * @description 描述一下方法的作用
    * @Date 21:24 2021/7/8
    * @Param  * @param id
    * @return com.jk.vo.ResultInfo
    **/
    @Override
    public ResultInfo queryOrderById(String id) {
        ResultInfo resultInfo = new ResultInfo();
        try {
            Query query = new Query();
            query.addCriteria(Criteria.where("id").is(id));
            resultInfo.setData(mongoTemplate.find(query,OrderPO.class));
            resultInfo.setCode(200);
            resultInfo.setMessage("订单详情成功");
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMessage("订单详情失败");
        }
        return resultInfo;
    }
/**
 *
 *
 * @author 赵磊荣
 * @date 2021/7/8 10:49
 * @return redis未付款订单
 */
    @Override
    public List<OrderPO> queryOrderRedis() {
         ArrayList<OrderPO> list = new ArrayList<>();
        list=(ArrayList<OrderPO>) redisTemplate.opsForHash().values("1111");
        return list;
    }

    /**
    * @Author  王宇超
    * @method  queryOrderEnd
    * @description 查询已付款清单
    * @Date 17:04 2021/7/8
    * @Param  * @param
    * @return java.util.List<com.jk.po.OrderPO>
    **/
    @Override
    public List<OrderPO> queryOrderEnd() {
        Query query = new Query();
        query.addCriteria(Criteria.where("orderstatus").is(1));
        return mongoTemplate.find(query,OrderPO.class);
    }
    @Override
    public void orderRedisMogdb() {
        ArrayList<OrderPO> list = new ArrayList<>();
        list=(ArrayList<OrderPO>) redisTemplate.opsForHash().values("1111");
        OrderPO orderPO = new OrderPO();
        for (OrderPO b : list) {
            orderPO.setOrdernumber(b.getOrdernumber());
            orderPO.setAreaname(b.getAreaname());
            orderPO.setTraveltime(b.getTraveltime());
            orderPO.setPhone(b.getPhone());
            orderPO.setEmail(b.getEmail());
            orderPO.setHotelprice(b.getHotelprice());
            orderPO.setOrderstatus(1);
            redisTemplate.opsForHash().delete("1111",b.getOrdernumber());
        }
    }

}
